package com.cskaoyan.service;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.PermissionsPostBO;
import com.cskaoyan.bean.bo.RoleBO;
import com.cskaoyan.bean.bo.RoleCreateBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.PermissionPO;
import com.cskaoyan.bean.po.RolePO;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public TVO<RolePO> queryAllRole(BaseParam baseParam, String name) {

        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<RolePO> roles = roleMapper.selectAllRoles(baseParam.getSort(), baseParam.getOrder(), name);
        PageInfo pageInfo = new PageInfo(roles);
        long total = pageInfo.getTotal();
        return TVO.data(total, pageInfo.getPages(), baseParam.getPage(), baseParam.getLimit(), roles);
    }

    @Override
    public BaseRespVo update(RoleBO roleBO) {
        int affectRows = roleMapper.updateRole(RoleBO.getRolePO(roleBO));
        return BaseRespVo.ok(null);
    }

    @Override
    public BaseRespVo delete(RoleBO roleBO) {
        int affectRows = roleMapper.logicalDeleteRole(RoleBO.getRolePO(roleBO));
        return BaseRespVo.ok(null);
    }

    @Override
    public BaseRespVo create(RoleCreateBO createBO) {
        RolePO rolePo = RoleCreateBO.getRolePo(createBO);
        int affectRows = roleMapper.insertRole(rolePo);
        RoleCreateVO roleCreateVO = RoleCreateVO.getRoleCreateVO(rolePo);
        return BaseRespVo.ok(roleCreateVO);
    }

    @Override
    public BaseRespVo permissions(Integer roleId) {
        List<String> assignedPermissions = null;
        if (roleId == 1) {
            assignedPermissions = roleMapper.selectAllPermission();
        } else {
            assignedPermissions = roleMapper.selectPermissionByRoleId(roleId);
        }
        RolePermissionVO rolePermissionVO = new RolePermissionVO();
        rolePermissionVO.setAssignedPermissions(assignedPermissions);
        List<FirstSystemPermissionsVO> firstSystemPermissionsVOS = roleMapper.selectSystemPermissionsByTypeId(0);

        for (FirstSystemPermissionsVO vo : firstSystemPermissionsVOS) {
            List<SecondSystemPermissionsVO> children = roleMapper.selectSystemPermissionsByPId1(vo.getUId());
            for (SecondSystemPermissionsVO child : children) {
                List<ThirdSystemPermissionsVO> ch = roleMapper.selectSystemPermissionsByPId2(child.getUId());
                child.setChildren(ch);
            }
            vo.setChildren(children);
        }
        rolePermissionVO.setSystemPermissions(firstSystemPermissionsVOS);
        return BaseRespVo.ok(rolePermissionVO);
    }

    @Override
    public BaseRespVo permissionsPost(PermissionsPostBO permissionsPostBO) {

        List<String> permissions = permissionsPostBO.getPermissions();
        Integer roleId = permissionsPostBO.getRoleId();
        Integer rows = roleMapper.deletePermissionsByRoleId(roleId);
        for (String permission : permissions) {
            PermissionPO permissionPO = new PermissionPO();
            permissionPO.setAddTime(new Date());
            permissionPO.setPermission(permission);
            permissionPO.setDeleted(false);
            permissionPO.setUpdateTime(new Date());
            permissionPO.setRoleId(roleId);
            roleMapper.insertPermissionByRoleId(permissionPO);
        }
        return BaseRespVo.ok(null);
    }

    @Override
    public List<String> selectPermissionByRoleId(Integer roleId) {
        List<String> strings = roleMapper.selectPermissionByRoleId(roleId);
        for (String string : strings) {
            if (string.contains("*")) {
                return roleMapper.selectAllPermission();
            }
        }
        return strings;
    }
}
